package com.woniu.inlet.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniu.inlet.fo.FundGraphVo;
import com.woniu.inlet.vo.FundCurrencyVo;
import com.woniu.inlet.vo.FundExponentVo;
import com.woniu.inlet.vo.DataVo;
import com.woniu.inlet.vo.GraphVo;
import com.woniu.outlet.elasticSearch.po.Fund;
import com.woniu.outlet.mysql.po.FundRela;
import com.woniu.outlet.redis.po.FundRedis;
import com.woniu.service.command.*;
import com.woniu.util.ResponseResult;
import com.woniu.util.po.NetWorth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: my
 * @time: 2021/4/21 15:26
 */
@Slf4j
@RestController
@CrossOrigin
@Api("基金信息获取接口")
public class FundController {
    @RequestMapping(value = "/product/fund/manage",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult fundManage(String status,Integer pageIndex,Integer pageNum){
        if (status == null || "y".equals(status)){
            status = "y";
        }else {
            status = "n";
        }
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        if (pageNum == null || pageNum < 1){
            pageNum = 5;
        }
        FundShowCommand command = new FundShowCommand(status,pageIndex,pageNum);
        List<FundRela> list = command.execute();
        return new ResponseResult(list,200);
    }

    @RequestMapping(value = "/product/fund/modify",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult upOrDownFund(Integer id){
        if (id == null || id < 1){
            return new ResponseResult(400,"参数有误");
        }
        ChangeFundStatusCommand command = new ChangeFundStatusCommand(id);
        command.execute();
        return ResponseResult.SUCCESS;
    }

    @RequestMapping(value = "/product/fund/allFund",method = {RequestMethod.POST,RequestMethod.GET})
    public List<DataVo> getAll(){
        FundAllCommand command = new FundAllCommand();
        List<Fund> list = command.execute();
        List<DataVo> vos = new ArrayList<>();
        for (Fund fund : list) {
            DataVo vo = new DataVo();
            vo.setCode_id(fund.getCode());
            vo.setName(fund.getName());
            vo.setDay_growth(fund.getDayGrowth());
            vo.setToday_valuation(new BigDecimal(fund.getNetWorth()));
            vos.add(vo);
            log.debug(vo.toString());
        }
        return vos;
    }

    @RequestMapping(value = "/product/fund/search",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("模糊查询基金")
    public ResponseResult searchFund(String name){
        SearchFundCommand command = new SearchFundCommand(name);
        List<Fund> list = command.execute();
        return new ResponseResult(list,200);
    }

    @RequestMapping(value = "/product/fund/exponent",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("获取指数型基金方法")
    public ResponseResult getExponentFund(Integer pageNum){
        if (pageNum == null){
            pageNum = 0;
        }
        FundQueryCommand command = new FundQueryCommand("指数型",pageNum);
        try {
            List<Fund> funds = command.execute();
            List<FundExponentVo> vos = new ArrayList<>();
            for (Fund fund : funds) {
                FundExponentVo vo = new FundExponentVo();
                vo.setCode(fund.getCode());
                vo.setPre(fund.getLastYearGrowth());
                vo.setType(fund.getName());
                vos.add(vo);
            }
            return new ResponseResult(vos,200);
        } catch (Exception e) {
            log.debug("基金信息获取失败");
            e.printStackTrace();
            return ResponseResult.FAIL;
        }
    }

    @RequestMapping(value = "/product/fund/currency",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("获取货币型基金方法")
    public ResponseResult getCurrencyFund(){
        FundQueryCommand command = new FundQueryCommand("货币型",0);
        try {
            List<Fund> funds = command.execute();
            List<FundCurrencyVo> vos = new ArrayList<>();
            for (Fund fund : funds) {
                FundCurrencyVo vo = new FundCurrencyVo();
                vo.setCode(fund.getCode());
                vo.setName(fund.getName());
                vo.setPre(fund.getLastYearGrowth());
                int index = (int) (Math.random() * 4);
                vo.setTag(FundCurrencyVo.TAGS[index]);
                vos.add(vo);
            }
            return new ResponseResult(vos,200);
        } catch (Exception e) {
            log.debug("基金信息获取失败");
            e.printStackTrace();
            return ResponseResult.FAIL;
        }
    }

    @RequestMapping(value = "/product/fund/detail",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("基金详情方法")
    @ApiImplicitParam(name = "code",value = "基金代码",required = true)
    public ResponseResult getFundDetailByCode(String code){
        FundDetailCommand command = new FundDetailCommand(code);
        FundRedis data = command.execute();
        JSONObject object = JSON.parseObject(data.getData());
        return new ResponseResult(object,200);
    }

    @RequestMapping(value = "/product/fund/all",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiImplicitParam(name = "type",value = "类型",required = false)
    @ApiOperation("根据类型获取基金")
    public ResponseResult getFunds(String type, Integer pageNum){
        if (type == null || "".equals(type.trim())){
            type = "all";
        }
        if (pageNum == null){
            pageNum = 0;
        }
        FundQueryCommand command = new FundQueryCommand(type,pageNum);
        try {
            List<Fund> funds = command.execute();
            return new ResponseResult(funds,200);
        } catch (Exception e) {
            log.debug("基金信息获取失败");
            e.printStackTrace();
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation("获取热门基金")
    @RequestMapping(value = "/product/fund/hot",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult getHotFunds(){
        HotFundCommand hotFundCommand = new HotFundCommand();
        List<Fund> funds = null;
        try {
            funds = hotFundCommand.execute();
            return new ResponseResult(funds,200);
        } catch (Exception e) {
            log.debug("热门基金获取失败");
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation("获取持仓详情")
    @RequestMapping(value = "/product/fund/position",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult getPosition(String code){
        FundPositionCommand fundPositionCommand = new FundPositionCommand(code);
        JSONObject execute = null;
        try {
            execute = fundPositionCommand.execute();
            return new ResponseResult(execute,200);
        } catch (Exception e) {
            log.debug("持仓数据获取失败");
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation("基金折线图数据获取")
    @RequestMapping(value = "/product/fund/graph",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseResult getFundGraph(FundGraphVo fundGraphVo){
        try {
            Map<String,GraphVo> map = new HashMap<>();
            int[] arr = {1,3,6,12,36};
            for (int i : arr) {
                fundGraphVo.setMonth(i);
                FundGraphCommand command = new FundGraphCommand(fundGraphVo);
                List<NetWorth> netWorths = command.execute();
                GraphVo vo = new GraphVo();
                GraphVo.GraphData data = new GraphVo.GraphData();
                List<String> categories = new ArrayList<>();
                List<GraphVo.GraphData> series = new ArrayList<>();
                List<String> datas = new ArrayList<>();
                data.setName("净值涨幅");
                for (NetWorth netWorth : netWorths) {
                    categories.add(netWorth.getDate());
                    datas.add(netWorth.getNetGains());
                }
                data.setData(datas);
                series.add(data);
                vo.setCategories(categories);
                vo.setSeries(series);
                String key = "";
                switch (i){
                    case 1:
                        key = "oneMonth";
                        break;
                    case 3:
                        key = "threeMonth";
                        break;
                    case 6:
                        key = "sixMonth";
                        break;
                    case 12:
                        key = "oneYear";
                        break;
                    case 36:
                        key = "threeYear";
                        break;
                    default:
                        break;
                }
                map.put(key,vo);
            }
            log.debug("数据获取成功");
            return new ResponseResult(map,200);
        } catch (URISyntaxException e) {
            log.debug("数据获取失败");
            return ResponseResult.FAIL;
        }
    }
}
